package com.library.lms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.library.lms.dao.BooksMapper;
import com.library.lms.entity.Books;
import com.library.lms.service.BooksService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class BooksServiceImpl  implements BooksService {
    @Resource
    public BooksMapper booksMapper;

    @Override
    public int addBooks(Books books) {
        return booksMapper.insertBooks(books);
    }

    @Override
    public List<Books> queryBooks() {
        return booksMapper.selectBooks();
    }

    @Override
    public List<Books> queryBooksByName(String name, String author) {
        List<Books> books = booksMapper.selectBooksByName(name, author);
        return books;
    }

    @Override
    public PageInfo<Books> findByPageService(int pageCode, int pageSize) {
        //分页插件
        PageHelper.startPage(pageCode, pageSize);
        //调用分页查询方法，mybatis自动进行分页计算
        List<Books> books = booksMapper.selectBooks();

        return new PageInfo<>(books);
    }

    @Override
    public int deleteBook(int id) {
        return booksMapper.deleteBook(id);
    }

    @Override
    public int editBook(Books books) {
        return booksMapper.updateBook(books);
    }

}
